package pubM

import (
	"fmt"
	metaD "db2s/MetaInit"
	"db2s/arg"
	"db2s/global"
	"db2s/go-log/log"
	"db2s/outPut"
	"db2s/parDef"
	"db2s/ref"
	mq "db2s/topic-mq"
	"sync/atomic"
)

type NewBreakPoint struct {
	BreakPointSum     *int64
	TableAccount      *int64
	TableFree         *int64
	ContinueTableList map[string]int
	ObjectFlow        chan any
}
type tabBaseMe struct {
	Col   any `json:"struct"`
	Index any `json:"index"`
	Part  any `json:"part"`
}
type SchemaInitContext struct {
	Seq               int
	Schema            string
	Table             string
	MSchema           string
	MTable            string
	TableMiss         bool
	ObjectLogSeq      int64
	TColumnMeta       global.TableMetaGather
	IndexData         metaD.IndexChooseResult
	Role              string
	TableSum          int
	IgnoreObjectSum   int
	TableFree         *int64
	TableAccount      *int64
	BreakPointSum     *int64
	TableAttributes   arg.TableSubOptions
	Meta              global.TableMetaS
	BaseMe            tabBaseMe
	Options           parDef.Options
	ChanSchemaContext chan any //chan *SchemaInitContext
	TPod              *outPut.TPod
}
type ObjectMetaDataPoint struct {
	NewBreakPoint
	ExecFunc global.EFunc
}

func schemaMetaExecQuitFunc(event string, _ any) (r any) {
	ref.MyWaitDel("metaCheck")
	return
}
func breakPointContinue(event string, f any) (r any) {
	//var event = "[breakPointContinue]"
	if f == nil || len(f.([]any)) == 0 {
		return
	}
	_, chanParameter, useParameter := schemaMetaExecFuncParameterSplit(f.([]any))
	v := chanParameter.(arg.InputTableAttributes)
	var tableName = fmt.Sprintf("%v.%v", v.Schema, v.Table)
	if _, ok := useParameter[1].(map[string]int)[tableName]; ok {
		log.MainLog().Info(fmt.Sprintf("%v When the breakpoint resume function is enabled, the current table %v will be skipped.", event, tableName))
		atomic.AddInt64(useParameter[0].(*int64), 1)
		return true
	}
	return false
}
func newSchemaInitContextSubOptions(v arg.InputTableAttributes) (res parDef.Options, err error) {
	//var pos any
	//if pos, err = full.GetGolPosition(full.Gol{
	//	Drive: []string{GlobalPConfigs.dSns.SrcDBName},
	//	Join:  "left",
	//	Parameter1: []parDef.Parameter{
	//		{Con: GlobalPConfigs.db.Source.GetDB("single"), Options: parDef.Options{Scn: GlobalPConfigs.rules.Sync.Scn}},
	//	},
	//}); err != nil {
	//	return
	//}
	//if err = Store.Insert(Store.Position, Store.PositionInfo{
	//	Key:        fmt.Sprintf("%v_%v", "AA", GlobalPConfigs.dSns.SrcDBName),
	//	ObjectName: "AA",
	//	TimeStamp:  time.Now().Format("2006-01-02T15-04-05"),
	//	Drive:      GlobalPConfigs.dSns.SrcDBName,
	//	Position:   fmt.Sprintf("%v", pos),
	//}); err != nil {
	//	fmt.Println("--------err:", err)
	//	return
	//}
	//return parDef.Options{
	//	Scn: fmt.Sprintf("%v", pos),
	//	//StopTime: time.Duration(GlobalPConfigs.rules.Sync.),
	//	WhereSql:     v.TableAttributes.WhereSql,
	//	WhereAdd:     v.TableAttributes.WhereAdd,
	//	SqlMode:      GlobalPConfigs.rules.Sync.SqlMode,
	//	TablePx:      v.TableAttributes.PX,
	//	SqlLogBin:    GlobalPConfigs.rules.Sync.SqlLogBin,
	//	RowsLimit:    GlobalPConfigs.rules.Sync.RowsLimit,
	//	RepairMethod: GlobalPConfigs.repair.DataFix,
	//}, err
	return
}
func newSchemaInitContext(seq int, v arg.InputTableAttributes, logSeq int64) (result *SchemaInitContext, err error) {
	var options parDef.Options
	if options, err = newSchemaInitContextSubOptions(v); err != nil {
		return
	}
	return &SchemaInitContext{
		Schema: v.Schema, Table: v.Table, MSchema: v.MSchema,
		MTable: v.MTable, Seq: seq, ObjectLogSeq: logSeq,
		Role: v.TableAttributes.Role, TableAttributes: v.TableAttributes,
		Options: options,
	}, err
}
func objectMetaInit(seq int, v arg.InputTableAttributes, logSeq int64) (res *SchemaInitContext, err error) {
	//if res, err = newSchemaInitContext(seq, v, logSeq); err != nil {
	//	return
	//}
	//if t.objectMetaInitAbnormalQuit(seq, v, res) {
	//	res.TableMiss = true
	//	return
	//}
	//for _, p := range ref.GetJSONFieldNames(res.BaseMe) {
	//	fun, ok := getObjectMetaFunc()[p]
	//	if !ok {
	//		continue
	//	}
	//	if err = t.objectMetaResult(logSeq, p, v, fun.Call(reflectValue(v)), res); err != nil {
	//		err = ref.ErrAddPrintf("objectMetaInit", err)
	//		return
	//	}
	//}
	return
}
func schemaMetaExecFunc(event string, f any) (r any) {
	//if f == nil || len(f.([]any)) == 0 {
	//	return
	//}
	//baseParameter, chanParameter, userParameter := schemaMetaExecFuncParameterSplit(f.([]any))
	//if _, ok := baseParameter["seq"]; !ok {
	//	return
	//}
	//if _, ok := baseParameter["logSeq"]; !ok {
	//	return
	//}
	//seq := baseParameter["seq"].(int)
	//logSeq := baseParameter["logSeq"].(int64)
	//if p, err := objectMetaInit(seq, chanParameter.(arg.InputTableAttributes), logSeq); err != nil {
	//	log.ErrorLog().Error(fmt.Sprintf("%v", err))
	//	return
	//} else {
	//	//ChanSchemaContext := userParameter[0].(*SchemaInitContext)
	//	atomic.AddInt64(userParameter[0].(*int64), 1)
	//	atomic.AddInt64(userParameter[1].(*int64), 1)
	//	ChanSchemaContext.ChanSchemaContext <- p
	//}
	return
}
func ObjectMetaDataInit(point ObjectMetaDataPoint) mq.MonitorMsgMq {
	ref.MyWaitAdd("metaCheck")
	return mq.NewMonitorMsgMq1().SetGeneralSeqLog().SetSendMsg(point.ObjectFlow).SetNormalQuitFunc(global.EFunc{
		FuncName: schemaMetaExecQuitFunc}).SetAbnormalQuitFunc(global.EFunc{
		FuncName: schemaMetaExecQuitFunc}).SetContinueFunc(global.EFunc{
		FuncName: breakPointContinue, Params: []any{point.BreakPointSum, point.ContinueTableList}}).SetExecFunc([]global.EFunc{
		//{FuncName: schemaMetaExecFunc, Params: []any{point.TableAccount, point.TableFree}}
		point.ExecFunc,
	})
}
